# -*- coding: utf-8 -*-
import scrapy
from webspider.items import job_2spiderItem
import re

class Job2Spider(scrapy.Spider):
    name = 'job_all'
    allowed_domains = ['jobs.51job.com']
<<<<<<< HEAD
    start_urls = ['https://jobs.51job.com/houduankaifa/p1/']
=======
    start_urls = ['http://jobs.51job.com/houduankaifa/p1/']
>>>>>>> a14f5150eb56dfc65bb96f2ac8b48776d600553f

    def parse(self, response):
        job_list = response.xpath("//div[@class='e ']")
        for job in job_list:
            item = job_2spiderItem()
            item['name'] = job.xpath('.//a/@title').extract_first()
            item['url'] = job.xpath('.//a/@href').extract_first()
            item['company'] = job.xpath(".//a/@title").extract()[1]
            item['salary'] = job.xpath(".//span[@class='location']/text()").extract_first()
            item['release_time'] = job.xpath(".//span[@class='time']/text()").extract_first()
            item['location'] = job.xpath(".//span[@class='location name']/text()").extract_first()
            item['edu_background'] = re.search("学历要求：(.*?)<span>",job.xpath(".//p[@class='order']").extract_first()).group(1)
            item['work_exp'] = re.search("工作经验：(.*?)<span>",job.xpath(".//p[@class='order']").extract_first()).group(1)
            item['company_nature'] = re.search("公司性质：(.*?)<span>",job.xpath(".//p[@class='order']").extract_first()).group(1)
            item['company_scale'] = re.search("公司规模：(.*?)</p>",job.xpath(".//p[@class='order']").extract_first()).group(1)
            yield item
        try:
            page_count = int(response.xpath("//div[@class='dw_page']//input/@value").extract_first())
            current_page = int(response.xpath("//div[@class='dw_page']//input/@value").extract()[1])
            if current_page < page_count:
                next_url = "http://jobs.51job.com/houduankaifa/p{}/".format(current_page+1)
                print(next_url)
                yield scrapy.Request(
                    next_url,
                    callback=self.parse
                )
        except:
            print("error")



